package com.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.org.bean.system.UserInfo;
import com.org.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction {
	/**
	 * 用户信息初始化，添加用户，删除用户
	 */
	@Autowired
	private UserService userService;
	private Iterator<String> iter;
	private List<String> list;

	private Map<String, Object> status(List<UserInfo> userInfos) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (userInfos.size() != 0) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
			m.put("result", userInfos);
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");

		}
		return m;
	}

	private Map<String, Object> status(Boolean userInfos) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (userInfos) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");

		}
		return m;
	}

	@RequestMapping("/findAll")
	public @ResponseBody Map<String, Object> getUserInfos() {
		List<UserInfo> userInfos = new ArrayList<UserInfo>();
		userInfos = userService.findAll();
		return status(userInfos);
	}

	@RequestMapping("/{userId}/findById")
	public @ResponseBody Map<String, Object> getUserInfo(
			@PathVariable int userId) {
		List<UserInfo> userInfo = new ArrayList<UserInfo>();
		userInfo = userService.findById(userId);
		return status(userInfo);
	}
	@RequestMapping("/{userName}/findByName")
	public @ResponseBody Map<String, Object> getUserInfoByName(
			@PathVariable String userName) {
		List<UserInfo> userInfo = new ArrayList<UserInfo>();
		userInfo = userService.findByName(userName);
		return status(userInfo);
	}

	@RequestMapping("/addUser")
	public @ResponseBody Map<String, Object> addUser(
			@RequestBody Map<String, Object> userMap) {
		boolean result;
		iter = userMap.keySet().iterator();
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName((String) userMap.get(list.get(0)));
		userInfo.setPassword((String) userMap.get(list.get(1)));
		userInfo.setContact((String) userMap.get(list.get(2)));
		userInfo.setIsAdmin((Boolean) userMap.get(list.get(3)));
		userInfo.setRemark((String) userMap.get(list.get(4)));
		result = userService.addUser(userInfo);
		return status(result);
	}

	@RequestMapping("/updateUser")
	public @ResponseBody Map<String, Object> updateUser(
			@RequestBody Map<String, Object> userMap) {
		boolean result;
		iter = userMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId((Integer) userMap.get(list.get(0)));
		userInfo.setIsAdmin((Boolean) userMap.get(list.get(1)));
		result = userService.modifyUser(userInfo);
		return status(result);
	}
	
	@RequestMapping("/modifyUser")
	public @ResponseBody Map<String, Object> modifyUser(
			@RequestBody Map<String, Object> userMap) {
		boolean result;
		iter = userMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId((Integer) userMap.get(list.get(0)));
		userInfo.setPassword( (String) userMap.get(list.get(1)));
		userInfo.setContact( (String) userMap.get(list.get(2)));
		userInfo.setRemark( (String) userMap.get(list.get(3)));
		result = userService.modifyUser(userInfo);
		return status(result);
	}

	@RequestMapping("/{userId}/deleteUser")
	public @ResponseBody Map<String, Object> deleteUser(@PathVariable int userId) {
		boolean result;
		result = userService.deleteUser(userId);
		return status(result);
	}

}
